package cn.mangodai.service.impl;

import cn.mangodai.dao.UserMapping;
import cn.mangodai.domain.User;
import cn.mangodai.service.UserService;
import cn.mangodai.vo.Message;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * MangoDai
 * 2017 09 23 9:11 PM
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    UserMapping userMapping;

    @Override
    public Message register(User user) {
        Message msg = new Message();
        int j = userMapping.checkCorrect(user.getSchoolNumber(), user.getName());
        if (0 == j) {
            msg.setState(400);
            msg.setMsg("学号与姓名不匹配: " + user.getName() + "，学号:" + user.getSchoolNumber());
        } else {
            int i = userMapping.checkExists(user.getSchoolNumber());
            if (0 == i) {
                int i1 = userMapping.insertSelective(user);
                if (i1 == 1) {
                    msg.setState(200);
                    msg.setMsg("注册成功");
                } else {
                    msg.setMsg("注册失败 : " + user.toString());
                }
            } else {
                msg.setState(400);
                msg.setMsg(user.getName() + " ，您已经注册过了 : ");
            }
        }
        return msg;
    }

    @Override
    public List<User> getAllUser() {
        return userMapping.getUser();
    }
}
